
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  basicprocess.authentication;


import java.util.Hashtable;
import java.util.Vector;

import basicprocess.authentication.tablename.Authentication;

import logicalOperation.Logicaloperation;

import pool.tablename.Property_details;

import utility.CapitalChar;
import utility.ConvertToLower;
import utility.Input;
import validation.Validation;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;



public class Condition implements  basicprocess.authentication.Authentication 
{
	public basicprocess.Authentication authentication;
	public DBManager database;
	String object;
	GetIdName gid;
	Hashtable<Object, Object> table = new Hashtable<Object, Object>();
	String CONDITIONFIELD="conditionfield";
	String CONDITIONVALUE="conditionvalue";
	String CONDITIONOPERATOR="Conditionoperator";
	String VALIDATION="validation";
	String CONDITION="condition";
	String LOGICALOPERATOR="Logicaloperator";
	RecordSearch sr;
	String ACCESS="access";
	String access="";
	public Vector<Object> property = new Vector<Object>();
	public Hashtable<Object, Object> basichashtable =new Hashtable<Object, Object>();
	public Hashtable<Object, Object> Detailhashtable =new Hashtable<Object, Object>();
	
	public void initializeData() 
	{
		table.clear();
		String MasterPath = Input.MASTERREPORTSPATH + object +"//" + object + "_mid" + Input.FILRFORMAT;
		table.put("mid", object);
		sr.setConditionMap(table);
		String line[] = sr.getArrayFromResultSet();
		table.clear();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = splitline[Column.pv_index-1];
			if(value.equals("0"))
				value =  splitline[Column.vt_index-1];
			table.put(splitline[Column.pid_index-1], value);
		}
		table = ConvertToLower.convertHashKey(table);
		if(table.containsKey(ACCESS))
		{
			access = table.get(ACCESS).toString();
			try 
			{
				Integer.parseInt(access);
				access = gid.getItem(access);
			} catch (Exception e) 
			{
				access = table.get(ACCESS).toString();
			}
			if(access.equals("no$") || access.equals("0"))
				access = table.get(ACCESS).toString();
		}
	}
	
	public boolean getResult() 
	{
		String request1stchar = authentication.req.getProcess().substring(0, 1).toLowerCase();
		access = access.toLowerCase();
		
		boolean result=false;
		String logicaloperation ="";
		property.addAll(authentication.PropertyMap);
		basichashtable.putAll( authentication.MasterMap);
		//Detailhashtable.putAll( authentication.pool.getDetailHashtabel());
		
		basicprocess.authentication.tablename.Authentication auth;
		try 
		{
			Class c = Class.forName("basicprocess.authentication.tablename."+CapitalChar.makeFirstCharCapital(authentication.tablename));
			auth = (Authentication) c.newInstance(); 
			auth.setObject(this);
			auth.getResult();
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InstantiationException e) 
		{	
			e.printStackTrace();
		}
		catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		}
		do
		{	
			initializeData();
			if(logicaloperation.length()>0)
			{	
				try {
					Logicaloperation logicalopeartion;
					Class c = Class.forName("validation."+CapitalChar.makeFirstCharCapital(logicaloperation));
					logicalopeartion = (Logicaloperation)c.newInstance();
					result = logicalopeartion.getResult(result, processCondition());
				}
				catch (Exception e) 
				{	
					e.printStackTrace();
				}
			}
			else
				result = processCondition();
			if(table.containsKey(CONDITION))
			{
				object = table.get(CONDITION).toString();
				logicaloperation = table.get(LOGICALOPERATOR).toString();
			}	
			else
				break;
		}while(table.containsKey(CONDITION));
		
		access = access.toLowerCase();
		if(access.contains(request1stchar))
			return result;
		else
			return true;
	}

	public void setobject(Object object,String ConditionObject) 
	{
		authentication = (basicprocess.Authentication) object;
		this.database = authentication.database;
		sr = new RecordSearch();
		this.object = ConditionObject;
		gid =new GetIdName();
		CONDITIONFIELD = gid.getId(CONDITIONFIELD);
		CONDITIONVALUE = gid.getId(CONDITIONVALUE);
		CONDITION = gid.getId(CONDITION);
		CONDITIONOPERATOR = gid.getId(CONDITIONOPERATOR);
		VALIDATION = gid.getId(VALIDATION);
		LOGICALOPERATOR = gid.getId(LOGICALOPERATOR);
		ACCESS = gid.getId(ACCESS);
		initializeData();
	}
	
	public boolean processCondition()
	{	
		
		try {
			if(authentication.MasterMap.containsKey(gid.getItem(table.get(CONDITIONFIELD).toString())))
			{
				String operator=  table.get(CONDITIONOPERATOR).toString();
				String ConditionValue =  gid.getItem(table.get(CONDITIONVALUE).toString()).toLowerCase();
				String value = authentication.MasterMap.get(gid.getItem(table.get(CONDITIONFIELD).toString())).toString().toLowerCase();
				if(loadvalidaionclass(operator, value, ConditionValue))
					return false;
			}
			/*if(authentication.pool.getDetailHashtabel().containsKey(gid.getItem(table.get(CONDITIONFIELD).toString())))
			{
				String operator= table.get(CONDITIONOPERATOR).toString();
				String ConditionValue =  table.get(CONDITIONVALUE).toString();
				String value = authentication.pool.getDetailHashtabel().get(table.get(CONDITIONFIELD)).toString();
				if(loadvalidaionclass(operator, value, ConditionValue))
					return false;
			}*/
		} catch (Exception e) 
		{	
			e.printStackTrace();
		}
		
		try {
			for(int i=0;i<property.size();i++)
			{
				Hashtable<Object, Object >PropertyMap =new Hashtable<Object, Object>();
				
				//pd = (Property_details) property.get(i);
				PropertyMap = ConvertToLower.convertHashKey((Hashtable<Object, Object>)property.get(i));
				if(PropertyMap.get("pid").toString().equals(table.get(CONDITIONFIELD)))
				{
					String value="";
					if(PropertyMap.containsKey("pv"))
					{
				     	value = PropertyMap.get("pv").toString();
				     	 
					}	
					else
						value = PropertyMap.get("vt").toString();
					
					String operator= table.get(CONDITIONOPERATOR).toString();
					String ConditionValue =  table.get(CONDITIONVALUE).toString();
					if(ConditionValue.equals("no$"))
						ConditionValue =  table.get(CONDITIONVALUE).toString();
					if(loadvalidaionclass(operator, value, ConditionValue))
						return false;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}	
		/*if(property.size()>0)
			return true;*/
		return true;
	}
	
	
	
	public boolean loadvalidaionclass(String operator,String value,String ConditionValue)
	{
		Validation valid;
		String path = Input.MASTERREPORTSPATH + VALIDATION + "//" + VALIDATION + "_mid" + Input.FILRFORMAT;
		Hashtable<Object, Object> tempcondition =new Hashtable<Object, Object>();
		tempcondition.put("pid", operator);
		tempcondition.put("mid",VALIDATION);
		tempcondition.put("td", "null");
		sr.setConditionMap(tempcondition);
		String line[] =sr.getArrayFromResultSet();
		for(int k=0;k<line.length;k++)
		{
			String splitline[] =line[k].split("\t");
			String classname =splitline[Column.pv_index-1];
			if(classname.equals("0"))
				classname = splitline[Column.vt_index-1];
			else
				classname = gid.getItem(classname);
			try
			{
				Class c = Class.forName("validation."+CapitalChar.makeFirstCharCapital(classname));
				valid = (Validation)c.newInstance();
				if(ConditionValue.equals("0"))
					ConditionValue =  table.get(CONDITIONVALUE).toString();
				valid.initPool();
				return valid.validate(value, ConditionValue);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	 return false; 	
	}


}
